﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MyPhotoIndex.Settings
{
    delegate void KeyboardShortcutOperationDelegate();

    public class KeyboardMappings
    {
        private List<KeyboardMapping> m_mappings = new List<KeyboardMapping>();
        private Dictionary<string, KeyboardShortcutOperationDelegate> m_delegates = new Dictionary<string, KeyboardShortcutOperationDelegate>();

        public List<KeyboardMapping> Mappings
        {
            get { return m_mappings; }
            set { m_mappings = value; }
        }

        public KeyboardMappings()
        {
            KeyboardMapping kbm = new KeyboardMapping();
            kbm.OperationId = "{A03BFD6F-869F-4719-B225-7F6BF30CB5F1}";
            kbm.OperationName = "Assign Tags To selected images";
            kbm.Shortcut = (int)(Keys.Control | Keys.F2);

            m_mappings.Add(kbm);
        }

        internal bool HandleShortcut(Keys keyData)
        {
            foreach (KeyboardMapping mapping in m_mappings)
            {
                if (mapping.Shortcut == (int)keyData)
                {
                    KeyboardShortcutOperationDelegate functionDelegate = null;
                    if (m_delegates.TryGetValue(mapping.OperationId, out functionDelegate) == true &&
                        functionDelegate != null)
                    {
                        functionDelegate.Invoke();
                        return true;
                    }
                }
            }

            return false;
        }

        internal void RegisterKeyBoardEvent(string operationId, KeyboardShortcutOperationDelegate functionDelegate)
        {
            m_delegates.Add(operationId, functionDelegate);
        }
    }
}
